# -*- coding:utf-8 -*-
from utils import publicmethod, datagetter
from pages import BaiduLoginPage
import unittest, time, sys
sys.path.append("..")


class logintest(unittest.TestCase):

    def setUp(self):
        self.driver = publicmethod.PublicMethod()
        self.driver.wd.implicitly_wait(30)
        self.driver.max_window()
        self.baidu_login_page = BaiduLoginPage.BaiduLoginPage(self.driver)
        self.driver.open_link(self.baidu_login_page.test_url)

    def test_01_login(self):
        datas = datagetter.get_xlsx_to_list('login.xlsx')
        for data in datas:
            self._login_system(*data)

    def test_02_register(self):
        print '打开注册页面'
        self.baidu_login_page.open_login_page()
        self.baidu_login_page.click_register_button()
        self.driver.switch_to_new_window()
        self.assertEqual(self.driver.get_title(), u'注册百度帐号')


    def tearDown(self):
        self.driver.quit()

    def _login_system(self, description, username, password, expacted):
        driver = self.driver
        baidu_login_page = self.baidu_login_page
        print 'case description', description
        driver.open_link(baidu_login_page.test_url)
        baidu_login_page.open_login_page()
        baidu_login_page.input_username(username)
        baidu_login_page.input_password(password)
        baidu_login_page.click_login()
        time.sleep(0.2)
        actual_text = baidu_login_page.get_error_text()
        self.assertEqual(actual_text, expacted)


if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(logintest('test_01_login'))
    suite.addTest(logintest('test_02_register'))
    unittest.TextTestRunner().run(suite)